/*
 * 版权所有 (c) 2022-2099 TBIT。保留所有权利。
 */

package com.tbit.uqbike.service.business.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.object.pojo.Adaccount;
import com.tbit.uqbike.object.pojo.AdaccountPopUp;
import com.tbit.uqbike.object.pojo.vo.AdaccountPopUpQueryVO;
import com.tbit.uqbike.object.pojo.vo.AdaccountPopUpVO;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.service.business.AdaccountPopUpService;
import com.tbit.uqbike.service.business.AdaccountService;
import com.tbit.uqbike.webmanager.dao.core.AdaccountPopUpDao;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 弹框广告
 *
 * @author yao
 * @since 2024-03-28
 */
@Service
@AllArgsConstructor
public class AdaccountPopUpServiceImpl implements AdaccountPopUpService {

    private final AdaccountPopUpDao adaccountPopUpDao;

    private final AdaccountService adaccountService;

    @Override
    @SuppressWarnings("resource")
    public PageInfo<AdaccountPopUp> page(AdaccountPopUpQueryVO queryVO) {
        return PageHelper.startPage(queryVO.getPageNO(), queryVO.getRowCount())
                .doSelectPageInfo(() -> adaccountPopUpDao.selectList(queryVO));
    }

    @Override
    public AdaccountPopUp getById(Long id) {
        return adaccountPopUpDao.selectById(id);
    }

    @Override
    public void save(AdaccountPopUpVO vo) {
        AdaccountPopUp adaccountPopUp = BeanUtil.copyProperties(vo, AdaccountPopUp.class);

        adaccountPopUpDao.insert(adaccountPopUp);
    }

    @Override
    public void update(AdaccountPopUpVO vo) {
        AdaccountPopUp adaccountPopUp = BeanUtil.copyProperties(vo, AdaccountPopUp.class);

        adaccountPopUpDao.updateById(adaccountPopUp);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        adaccountPopUpDao.deleteByIds(idList);
    }

    @Override
    public void sync(LoginInfo loginInfo) {
        // 获得弹框广告
        List<Adaccount> adaccountList = adaccountService.getListByType(11);

        // 收集弹框广告
        List<AdaccountPopUp> adaccountPopUpList = adaccountList.stream().map(adaccount -> {
            AdaccountPopUp adaccountPopUp = new AdaccountPopUp();

            adaccountPopUp.setAccountId(adaccount.getAccountId());

            adaccountPopUp.setTitle(adaccount.getTitle());

            adaccountPopUp.setCreator(loginInfo.getName());
            adaccountPopUp.setCreateTime(adaccount.getUpdateTime());
            adaccountPopUp.setUpdater(loginInfo.getName());
            adaccountPopUp.setUpdateTime(adaccount.getUpdateTime());

            adaccountPopUp.setStartTime(adaccount.getStartTime());
            adaccountPopUp.setEndTime(adaccount.getEndTIme());

            adaccountPopUp.setSort(adaccount.getSort());

            Integer type = convertType(adaccount.getOpenURL());

            String remark = StrUtil.isNotBlank(adaccount.getRemark()) ? adaccount.getRemark() : "";

            // 外部链接
            String url = "";
            if (type == 5) {
                url = adaccount.getOpenURL();
            }

            String content = "{\"popUpType\":\"2\"," +
                    "\"popUpTheme\":\"" + adaccount.getTitle() + "\"," +
                    "\"noticeTitle\":\"" + adaccount.getTitle() + "\"," +
                    "\"popUpFrequency\":\"2\"," +
                    "\"closeButtonPosition\":\"2\"," +
                    "\"titleFontSize\":16," +
                    "\"titleFontColor\":\"#000000\"," +
                    "\"content\":\"\"," +
                    "\"contentFontSize\":12," +
                    "\"contentFontColor\":\"#000000\"," +
                    "\"backgroundIcon\":\"\"," +
                    "\"backgroundColor\":\"#ffffff\"," +
                    "\"buttonText\":\"确认\"," +
                    "\"buttonFontSize\":12," +
                    "\"buttonFontColor\":\"#ffffff\"," +
                    "\"buttonBackgroundColor\":\"#5a9cf8\"," +
                    "\"routerPush\":\"\"," +
                    "\"url\":\"\"," +
                    "\"readRadio\":\"0\"," +
                    "\"readText\":\"勾选阅读内容\"," +
                    "\"readTextFontSize\":12," +
                    "\"readTextFontColor\":\"#000000\"," +
                    "\"readSwitch\":\"0\"," +
                    "\"readTime\":5," +
                    "\"urlList\":[{\"routerPush\":\"" + type + "\"," +
                    "\"url\":\""+url+"\"," +
                    "\"imageUrl\":\"" + adaccount.getImageId() + "\"}]," +
                    "\"autoPlayTime\":0," +
                    "\"oneClickClose\":\"1\"," +
                    "\"oneClickCloseIndex\":0," +
                    "\"remark\":\"" + remark + "\"}";

            adaccountPopUp.setContent(content);

            return adaccountPopUp;
        }).collect(Collectors.toList());


        // 保存
        adaccountPopUpList.forEach(adaccountPopUpDao::insert);
    }


    private Integer convertType(String openURL) {
        int type;

        if (StrUtil.isBlank(openURL)) {
            type = 0;
        } else if ("recharge".equals(openURL)) {
            type = 1;
        } else if ("vip".equals(openURL)) {
            type = 2;
        } else if ("depositvip".equals(openURL)) {
            type = 3;
        } else if ("ridecard".equals(openURL)) {
            type = 4;
        } else {
            type = 5;
        }

        return type;
    }

}